import * as Core from '@alicloud/pop-core';
// import { ConfigOptions } from '@alicloud/pop-core';

// 短信发送配置接口
export interface SmsConfig {
  accessKeyId: string;
  accessKeySecret: string;
  endpoint?: string;
  apiVersion?: string;
}

// 短信发送参数接口
export interface SendSmsParams {
  PhoneNumbers: string;
  SignName: string;
  TemplateCode: string;
  TemplateParam?: string;
}

export class SmsSender {
  private client: Core;
  private config: SmsConfig;

  constructor(config: SmsConfig) {
    this.config = {
      endpoint: 'https://dysmsapi.aliyuncs.com',
      apiVersion: '2017-05-25',
      ...config,
    };

    // 初始化阿里云短信服务客户端
    this.client = new Core({
      accessKeyId: this.config.accessKeyId,
      accessKeySecret: this.config.accessKeySecret,
      endpoint: this.config.endpoint,
      apiVersion: this.config.apiVersion,
    });
  }

  /**
   * 发送短信
   * @param params 短信参数
   */
  async sendSms(params: SendSmsParams): Promise<any> {
    try {
      const requestOption = {
        method: 'POST',
      };
      console.log('params', params);
      // 调用阿里云短信API发送短信
      const result = await this.client.request(
        'SendSms',
        params,
        requestOption,
      );
      console.log(`短信发送结果: ${JSON.stringify(result)}`);
      return result;
    } catch (error) {
      console.error('短信发送失败:', error);
      throw error;
    }
  }

  /**
   * 发送短信
   * @param phoneNumbers 手机号码，多个号码用逗号分隔
   * @param signName 短信签名
   * @param templateCode 短信模板ID
   * @param code 验证码
   */
  async sendVerificationCode(
    phoneNumbers: string,
    signName: string,
    templateCode: string,
    username: string,
    imei: string,
    warn: string,
  ): Promise<any> {
    const params: SendSmsParams = {
      PhoneNumbers: phoneNumbers,
      SignName: signName,
      TemplateCode: templateCode,
      TemplateParam: JSON.stringify({ username, imei, warn }),
    };

    return this.sendSms(params);
  }
}

// 使用示例
// async function example() {
//   // 配置阿里云AccessKey信息
//   const config: SmsConfig = {
//     accessKeyId: 'LTAI5tEidvcjyvfvLvSUBT3i',
//     accessKeySecret: 'CDg4BZiqnQPEqbUCZHlwzoOexjSeiX',
//   };

//   // 创建短信发送实例
//   const smsSender = new SmsSender(config);

//   try {
//     // 发送验证码短信
//     const result = await smsSender.sendVerificationCode(
//       '13800138000', // 接收短信的手机号码
//       '深圳市金盛昌科技', // 短信签名
//       'SMS_189714706', // 短信模板ID
//       '123456', // 验证码
//     );

//     console.log('短信发送成功:', result);
//   } catch (error) {
//     console.error('短信发送失败:', error);
//   }
// }

// 运行示例
// example();
